package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Collect;
import com.zjitc.pigspring.bean.po.CollectVO;
import com.zjitc.pigspring.bean.po.Type;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.CollectService;
import com.zjitc.pigspring.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/collect")
@Slf4j
public class CollectController {

    @Autowired
    private CollectService collectService;

    @GetMapping("/getByUser/{id}")
    public Result<List<CollectVO>> getCollectByUser(@PathVariable Integer id){
        List<CollectVO> list = collectService.getByUser(id);
        if (list==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(list);
        }
    }

    @GetMapping("/getAll")
    public Result<List<CollectVO>> getAll(){
        List<CollectVO> collects = collectService.getAll();
        if (collects!=null){
            return Result.success(collects);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Collect> selectCollectById(@PathVariable Integer id){
        Collect collect = collectService.getById(id);
        if(collect!=null){
            return Result.success(collect);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addCollect(@RequestBody Collect collect){
        QueryWrapper<Collect> wrapper = new QueryWrapper<Collect>()
                .eq("user_id",collect.getUserId())
                .eq("good_id",collect.getGoodId());
        Collect one = collectService.getOne(wrapper);
        if(one!=null){
            return Result.error(Codes.GOOD_EXISTS);
        }
        boolean save = collectService.save(collect);
        if(save){
            return Result.success(null, Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteCollect(@PathVariable Integer id){
        boolean flag = collectService.removeById(id);
        if (flag){
            return Result.success(null,Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PostMapping("/deleteAll")
    public Result deleteAllCollect(@RequestBody List<Integer> ids){

        boolean flag = collectService.removeBatchByIds(ids);
        if(flag){
            return Result.success(null,Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PutMapping("/updateOne")
    public Result updateCollect(@RequestBody Collect collect){

        QueryWrapper<Collect> wrapper = new QueryWrapper<Collect>()
                .eq("user_id",collect.getUserId())
                .eq("good_id",collect.getGoodId());
        Collect one = collectService.getOne(wrapper);
        if(one!=null){
            return Result.error(Codes.GOOD_EXISTS);
        }
        boolean flag = collectService.updateById(collect);
        if(flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<CollectVO>> queryCollect(@RequestBody CollectVO collect){

        List<CollectVO> list = collectService.getListByUsername(collect);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }
}
